package com.hzw.saas.common.verify.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.AccountValidatorUtil;
import com.hzw.saas.common.util.Json;
import com.hzw.saas.common.verify.config.SmsAliyunConfig;
import com.hzw.saas.common.verify.enums.SmsType;
import com.hzw.saas.common.verify.service.ISmsService;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 发送短信验证码 （阿里云实现）
 * </p>
 *
 * @author sonam
 * @since 2020-12-22
 */
@RequiredArgsConstructor
@Slf4j
public class AliyunSmsServiceImpl implements ISmsService {

    private final SmsAliyunConfig config;

    @Override
    public String send(String phone, String content) {
        // TODO: ZZL@01/13/2021
        return null;
    }

    @Override
    public String sendVerifyCode(String phone, String code, SmsType smsType) {
        if (!StringUtils.hasText(phone) || !AccountValidatorUtil.isMobile(phone)) {
            return null;
        }

        DefaultProfile profile = DefaultProfile.getProfile(config.getRegionId(), config.getAccessKey(), config.getSecretKey());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", config.getRegionId());
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", config.getSignName());
        switch (smsType) {
            case RESET:
                request.putQueryParameter("TemplateCode", config.getTemplateCodeReset());
                break;
            case REGISTERED:
                request.putQueryParameter("TemplateCode", config.getTemplateCodeRegister());
                break;
            case PAY:
                request.putQueryParameter("TemplateCode", config.getTemplateCodePayment());
                break;
            case CHANGE:
                request.putQueryParameter("TemplateCode", config.getTemplateCodeChange());
                break;
            default:
                AssertUtil.assertThrow("发送短信验证码失败, 请设置正确的Type", true);
        }
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        request.putQueryParameter("TemplateParam", Json.toJsonString(params));

        try {
            log.debug("[阿里云]发送验证短信{to: {}, code: {})...", phone, code);
            CommonResponse response = client.getCommonResponse(request);
            if (response.getHttpStatus() == HttpStatus.OK.value()) {
                return code;
            } else {
                return null;
            }
        } catch (ClientException e) {
            log.error("[阿里云]短信服务异常", e);
        }
        return null;
    }


}
